using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using System.Data.SqlClient;
using System.Data;

namespace SqlTools.SampleDAL.Data
{
	public class DbPage : DbBase<PagePrimaryKey, Page>
	{
		public DbPage()
			: base("daltest")
		{
		}

		public DbPage(string connectionStringName)
			: base(connectionStringName)
		{
		}

		#region Select
		public override Page SelectByPK(PagePrimaryKey key)
		{
			Page page = null;
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_Page_SelectByPK", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@PageID", SqlDbType.Int).Value = key.PageID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				if ( sdr.Read() )
				{
					page = Page.CreateFromReader(sdr);
					page._ConnectionStringName = _connectionStringName;
				}
				sdr.Close();

				conn.Close();
			}

			return page;
		}

		public DbList<Page> SelectByParentPageID(int parentPageID)
		{
			DbList<Page> pages = new DbList<Page>();
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_Page_SelectByParentPageID", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@ParentPageID", SqlDbType.Int).Value = parentPageID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				while ( sdr.Read() )
				{
					Page page = Page.CreateFromReader(sdr);
					page._ConnectionStringName = _connectionStringName;
					pages.Add(page);
				}
				sdr.Close();

				conn.Close();
			}

			return pages;
		}
		#endregion

		#region Insert
		public override PagePrimaryKey Insert(Page page, System.Data.SqlClient.SqlConnection conn, System.Data.SqlClient.SqlTransaction trans)
		{
			page._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Page_Insert", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@PageID", SqlDbType.Int).Direction = ParameterDirection.Output;
				cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = page.Name;
				cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = page.Title;

				if ( page.ParentPageID != 0 )
					cmd.Parameters.Add("@ParentPageID", SqlDbType.Int).Value = page.ParentPageID;
				else
					cmd.Parameters.Add("@ParentPageID", SqlDbType.Int);

				cmd.ExecuteNonQuery();

				if ( cmd.Parameters["@PageID"].Value != DBNull.Value )
				{
					page.Key.PageID = (int)cmd.Parameters["@PageID"].Value;
				}

				page._UpdateState(ObjectState.None);
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}

			return page.Key;
		}
		#endregion

		#region Update
		public override int Update(Page page, System.Data.SqlClient.SqlConnection conn, System.Data.SqlClient.SqlTransaction trans)
		{
			page._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Page_Update", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@PageID", SqlDbType.Int).Value = page.Key.PageID;
				cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = page.Name;
				cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = page.Title;

				if ( page.ParentPageID != 0 )
					cmd.Parameters.Add("@ParentPageID", SqlDbType.Int).Value = page.ParentPageID;
				else
					cmd.Parameters.Add("@ParentPageID", SqlDbType.Int);

				int rows = cmd.ExecuteNonQuery();

				page._UpdateState(ObjectState.None);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion

		#region Delete
		public override int Delete(Page page, System.Data.SqlClient.SqlConnection conn, System.Data.SqlClient.SqlTransaction trans)
		{
			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Page_Delete", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@PageID", SqlDbType.Int).Value = page.Key.PageID;

				int rows = cmd.ExecuteNonQuery();

				page._UpdateState(ObjectState.Deleted);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		} 
		#endregion
	}
}
